package com.leihao.marketing.controller;

import com.github.pagehelper.PageInfo;
import com.leihao.common.core.domain.R;
import com.leihao.marketing.domain.ListRequest;
import com.leihao.marketing.domain.Subject;
import com.leihao.marketing.service.SubjectService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("subject")
public class SubjectController {
    @Autowired
    private SubjectService subjectService;


    /**
     * 添加专题
     * @param subject
     * @return
     */
    @PostMapping("add")
    @ApiOperation(value = "添加专题")
    public R add(@Validated @RequestBody Subject subject) {
        subjectService.add(subject);
        return R.ok(null,"添加成功");
    }
    /**
     * 查询所有推荐专题
     * @return
     */
    @GetMapping("findAll")
    @ApiOperation(value = "查询所有推荐专题")
    public R<List<Subject>> findAll() {
        return R.ok(subjectService.findAll());
    }

    /**
     * 删除专题
     * @param subjectId
     * @return
     */
    @DeleteMapping("delete/{subjectId}")
    @ApiOperation(value = "删除专题")
    @ApiImplicitParam(name = "subjectId",value = "专题编号",required = true,dataType = "Integer")
    public R delete(@PathVariable Integer subjectId) {
        subjectService.delete(subjectId);
        return R.ok(null,"删除成功");
    }

    /**
     * 分页查询专题
     * @return
     */
    @PostMapping("findList")
    @ApiOperation(value = "分页查询专题")
    public R<PageInfo<Subject>> findList(@RequestBody ListRequest listRequest) {
        PageInfo<Subject> all = subjectService.findList(listRequest);
        return R.ok(all,"查询成功");
    }

    /**
     * 根据专题ID查询专题
     * @param subjectId
     * @return
     */
    @GetMapping("findById/{subjectId}")
    @ApiOperation(value = "根据专题ID查询专题")
    @ApiImplicitParam(name = "subjectId",value = "专题编号",required = true,dataType = "Integer")
    public R<Subject> findById(@PathVariable Integer subjectId){
        return R.ok(subjectService.findById(subjectId),"查询成功");
    }

    /**
     * 修改专题
     * @param subject
     * @return
     */
    @PutMapping("update")
    @ApiOperation(value = "修改专题")
    public R update(@Validated @RequestBody Subject subject) {
        subjectService.update(subject);
        return R.ok(null,"修改成功");
    }

}
